package com.cloud.school.app.controller;

import com.cloud.school.app.config.security.SecurityUserUtils;
import com.cloud.school.app.model.ImageModel;
import com.cloud.school.app.service.ImageService;
import com.cloud.school.app.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName FileController
 * @Description TODO
 * @Author 36297
 * @Date 2018/12/17 10:53
 * @Version 1.0
 */
@Api(tags = "文件管理控制器")
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private ImageService imageService;

    @ApiOperation("上传图片")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public ResponseUtil<ImageModel> upload(@ApiParam("上传文件") @RequestParam(name = "file") MultipartFile file) {
        if (file.isEmpty()) {
            ResponseUtil.fail("上传文件不能为空！");
        }
        ImageModel imageModel = imageService.save(file, SecurityUserUtils.getCurrentUserId());
        if (imageModel == null) {
            ResponseUtil.fail("文件上传失败！");
        }
        return ResponseUtil.ok(imageModel);
    }

    @ApiOperation("删除图片")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseUtil<Boolean> delete(@ApiParam("图片id") @RequestParam(name = "imageId") Long imageId) {
        return ResponseUtil.ok(imageService.delete(imageId));
    }

    @ApiOperation("查询图片")
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseUtil<ImageModel> query(@ApiParam("图片id") @RequestParam(name = "imageId") Long imageId) {
        return ResponseUtil.ok(imageService.seleteById(imageId));
    }
}
